/*
*************************************************************************
*西瑞克斯研发管理系统
*版 本: 1.0   
*日 期：02/12/2012    
*作 者： 王浩 QQ：873987060   
*************************************************************************   
*/ 
package com.manager.workflow.hander;

import java.util.List;

import org.fireflow.engine.EngineException;
import org.fireflow.engine.IWorkItem;
import org.fireflow.engine.IWorkflowSession;
import org.fireflow.engine.impl.TaskInstance;
import org.fireflow.engine.taskinstance.DynamicAssignmentHandler;
import org.fireflow.engine.taskinstance.IAssignable;
import org.fireflow.engine.taskinstance.IAssignmentHandler;
import org.fireflow.kernel.KernelException;

import com.application.usertable.entity.Usertable;
import com.manager.commons.CurrentUserUtils;
import com.manager.workflow.WorkFlowConstants;
import com.util.ApplicationUtil;
/**
 * 根据角色分配任务
 * @author 王浩 QQ：873987060   
 *
 */
public class RoleBasedAssignmentHandler implements IAssignmentHandler {

	@Override
	public void assign(IAssignable assignable, String performerName) throws EngineException,
			KernelException {
		//取得当前系统用户
		Usertable user = CurrentUserUtils.getCurrentUser();
		List<Usertable> users = null;
		ApplicationUtil au = new ApplicationUtil();
		TaskInstance taskInst = (TaskInstance)assignable;
		IWorkflowSession workSession = taskInst.getCurrentWorkflowSession();
		DynamicAssignmentHandler dynamicHandler = (DynamicAssignmentHandler) workSession.getAttribute("dynamicHandler");
		if(dynamicHandler!=null){
			dynamicHandler.assign(assignable, performerName);
		}else{
			String roleName = performerName==null?"":performerName.trim();
			IWorkItem workItem = null;
	        if(roleName.equals("yfjl")){
	        	roleName = WorkFlowConstants.ROLE_YFJL;
	        	users = au.getUserByName(roleName, user.getOid());
	        }
	        if(roleName.equals("yffz")){
	        	roleName = WorkFlowConstants.ROLE_YFZXFZR;
	        	users = au.getUserByName(roleName, user.getOid());
	        }
	        if(roleName.equals("fgfz")){
	        	roleName = WorkFlowConstants.ROLE_FGFZ;
	        	users = au.getUserByName(roleName,null);
	        }
	        if(users!=null && users.size()>0){
	        	for(Usertable us:users){
	        		workItem = assignable.asignToActor(us.getId());
	        		System.out.println(" === " + us.getId());
	        		if(workItem!=null){
	       	        	workItem.claim();
	       	        }
	        	}
	        }
		}
		
	}

}
